/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func deleteNode(root *TreeNode, key int) *TreeNode {
    if root == nil {
        return root
    }
    if root.Val == key {
        if root.Left == nil && root.Right == nil {
            return nil
        } else if root.Left == nil {
            return root.Right
        } else if root.Right == nil {
            return root.Left
        } else {
            miniNode := root.Right
            for miniNode.Left != nil {
                miniNode = miniNode.Left
            }
            miniNode.Left = root.Left
            root= root.Right
        }
    }
    if key < root.Val {
        root.Left = deleteNode(root.Left, key)
    }
    if key > root.Val {
        root.Right = deleteNode(root.Right, key)
    }
    return root
}